package com.adingxiong.cft.service.impl;

import com.adingxiong.cft.entity.TUser;
import com.adingxiong.cft.database.EsUserRepository;
import com.adingxiong.cft.mapper.TUserMapper;
import com.adingxiong.cft.service.EsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @ClassName EsUserServiceImpl
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/1/15 10:17
 **/

public class EsUserServiceImpl implements EsUserService {

    @Autowired
    private TUserMapper mapper;

    @Autowired
    private EsUserRepository repository;

    @Override
    public int importAll() {
        List<TUser> tUsers = mapper.selectList(null);
        Iterable<TUser> iterable = repository.saveAll(tUsers);
        Iterator<TUser> it = iterable.iterator();
        int result = 0;
        while (it.hasNext()){
            result ++;
            it.next();
        }

        return result;
    }

    @Override
    public void delete(Integer id) {
        repository.deleteById(id.longValue());
    }

    @Override
    public TUser creatte(Integer id) {
        TUser user = mapper.selectById(id);
        TUser save = repository.save(user);
        return save;
    }

    @Override
    public void batchDel(List<Integer> ids) {
        List<TUser> tUsers = mapper.selectBatchIds(ids);
        repository.deleteAll(tUsers);
    }

    @Override
    public Page<TUser> search(String keywords, Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum,pageSize);
        Page<TUser> byUsernameOrNickName = repository.findByUsernameOrNickName(keywords, keywords, pageable);
        return byUsernameOrNickName;
    }
}
